package com.jinke.api.modules.ums.controller;


import com.jinke.api.common.api.CommonPage;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.app.model.UserApi;
import com.jinke.api.modules.app.service.UserApiService;
import com.jinke.api.modules.ums.request.UserApiRequest;
import com.jinke.api.modules.ums.vo.UserApiVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * <p>
 * 配置表 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-16
 */
@RestController
@RequestMapping("/ums/user/api")
@Api(tags = "后台用户API管理")
@Tag(name = "后台用户API管理", description = "后台用户API管理")
public class UmsUserApiController {

    @Resource
    UserApiService userApiService;

    @ApiOperation("用户API列表")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<CommonPage<UserApiVO>> list(UserApiRequest request) {
        return CommonResult.success(CommonPage.restPage(userApiService.list(request)));
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Integer id, @RequestBody UserApi userApi) {
        if (userApi.getId() == null || userApi.getId() <= 0) {
            return CommonResult.failed();
        }
        boolean success = userApiService.updateById(userApi);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @ApiOperation("重置")
    @RequestMapping(value = "/reset/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Integer id) {
        UserApi userApi = new UserApi();
        userApi.setId(id);
        UUID uuid4 = UUID.randomUUID();
        userApi.setApiKey(uuid4.toString().replaceAll("-", ""));
        boolean success = userApiService.updateById(userApi);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

}

